package com.example.dormitorymanagement.controller;

import com.example.dormitorymanagement.model.Room;
import com.example.dormitorymanagement.service.RoomService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/buildings/{buildingId}/rooms")
@RequiredArgsConstructor
public class RoomController {

    private final RoomService roomService;

    @PostMapping
    public ResponseEntity<Room> createRoom(
            @PathVariable Integer buildingId,
            @RequestBody Room room) {
        Room createdRoom = roomService.createRoom(buildingId, room);
        return ResponseEntity.status(HttpStatus.CREATED).body(createdRoom);
    }

    @GetMapping
    public ResponseEntity<List<Room>> getAllRoomsByBuilding(@PathVariable Integer buildingId) {
        List<Room> rooms = roomService.getAllRoomsByBuilding(buildingId);
        return ResponseEntity.ok(rooms);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Room> getRoomById(@PathVariable Integer id) {
        Room room = roomService.getRoomById(id);
        return ResponseEntity.ok(room);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Room> updateRoom(
            @PathVariable Integer id,
            @RequestBody Room roomDetails) {
        Room updatedRoom = roomService.updateRoom(id, roomDetails);
        return ResponseEntity.ok(updatedRoom);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteRoom(@PathVariable Integer id) {
        roomService.deleteRoom(id);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/available")
    public ResponseEntity<List<Room>> getAvailableRooms(@PathVariable Integer buildingId) {
        List<Room> rooms = roomService.getAvailableRooms(buildingId);
        return ResponseEntity.ok(rooms);
    }

    @PatchMapping("/{id}/status")
    public ResponseEntity<Room> updateRoomStatus(
            @PathVariable Integer id,
            @RequestParam String status) {
        Room updatedRoom = roomService.updateRoomStatus(id, Room.RoomStatus.valueOf(status));
        return ResponseEntity.ok(updatedRoom);
    }
}